// Transform original data to fit the format of database
const ATCODER_STATUS_TRANSFORMER = {
  AC: "Accepted",
  WA: "Wrong Answer",
  MLE: "Memory Limit Exceeded",
  TLE: "Time Limit Exceeded",
  RE: "Runtime Error",
  CE: "Compilation Error",
  IE: "IE",
  Judging: "Testing",
  OLE: "Output Limit Exceeded",
  WJ: "Testing",
  WR: "Testing",
  QLE: "Crashed"
};

// rating = point * ATCODER_RATING_FACTOR
const ATCODER_RATING_FACTOR = 2

const transformPointToRating = (point) => {
  if(point === null || point > 4000){
    return null;
  }
  return point * ATCODER_RATING_FACTOR;
}

export default class AtCoderTransformer {
  static transformSubmissions(submissionArray, userId) {
    const atCoderSubmission = [];

    for (const submission of submissionArray) {
      const problemId = `${submission.contest_id}/${submission.problem_id}`;
      atCoderSubmission.push([
        submission.id,
        userId,
        submission.epoch_second,
        problemId,
        ATCODER_STATUS_TRANSFORMER[submission.result],
        transformPointToRating(submission.point),
      ]);
    }

    return {
      atCoderSubmission,
    };
  }

  static transformProblems(problemArray) {
    const atCoderProblems = [];

    for (const problem of problemArray) {
      const problemId = `${problem.contest_id}/${problem.id}`;
      atCoderProblems.push([
        problemId,
        problem?.name?.slice(0, 50),
        transformPointToRating(problem?.point)
      ]);
    }

    return {
      atCoderProblems,
    };
  }
}
